Test your skills through the online practice test: Microsoft .NET Quiz Online Practice Test

Related differences

Ques 36. What is a destructor?

A C# destuctor is not like a C++ destructor. It is actually an override for Finalize(). This
is called when the garbage collector discovers that the object is unreachable. Finalize()
is called before any memory is reclaimed.

Is it helpful? Add Comment View Comments
 

Ques 37. Can you use access modifiers with destructors?

No

Is it helpful? Add Comment View Comments
 

Ques 38. What is a delegate?

A delegate in C# is like a function pointer in C or C++. A delegate is a variable that calls
a method indirectly, without knowing its name. Delegates can point to static or/and
member functions. It is also possible to use a multicast delegate to point to multiple
functions.

Is it helpful? Add Comment View Comments
 

Ques 39. Write some code to use a delegate.

Member function with a parameter
using System;
namespace Console1
{
class Class1
{
delegate void myDelegate(int parameter1);
static void Main(string[] args)
{
MyClass myInstance = new MyClass();
myDelegate d = new myDelegate(myInstance.AMethod);
d(1); // <--- Calling function without knowing its name.
Test2(d);
Console.ReadLine();
}
static void Test2(myDelegate d)
{
d(2); // <--- Calling function without knowing its name.
}
}
class MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}
Multicast delegate calling static and member functions
using System;
namespace Console1
{
class Class1
{
delegate void myDelegate(int parameter1);
static void AStaticMethod(int param1)
{
Console.WriteLine(param1);
}
static void Main(string[] args)
{
MyClass myInstance = new MyClass();
myDelegate d = null;
d += new myDelegate(myInstance.AMethod);
d += new myDelegate(AStaticMethod);
d(1); //both functions will be run.
Console.ReadLine();
}
}
class MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}

Is it helpful? Add Comment View Comments
 

Ques 40. What is a delegate useful for?

The main reason we use delegates is for use in event driven programming.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users: